package com.e_manager.soft.emanager.service.impl;

import com.e_manager.soft.emanager.entity.Equipment;
import com.e_manager.soft.emanager.entity.InvitationRecord;
import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.entity.UserEquipment;
import com.e_manager.soft.emanager.idao.EquipmentDao;
import com.e_manager.soft.emanager.idao.InvitationRecordDao;
import com.e_manager.soft.emanager.idao.UserDao;
import com.e_manager.soft.emanager.idao.UserEquipmentDao;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.InvitationRecordService;
import com.e_manager.soft.emanager.token.TokenConstant;
import com.e_manager.soft.emanager.util.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [InvitationRecordController]<br>
 * 邀请记录信息<br>
 * <br>
 * [功能概要]<br>
 * 邀请记录信息<br>
 * <br>
 * [更新履历]<br>
 * 2018/04/03 ver1.00 JYS CORPORATION<br>
 *
 * @author JYS CORPORATION
 * @version 1.00
 */
@Service
public class InvitationRecordServiceImpl implements InvitationRecordService {
    @Autowired
    private InvitationRecordDao invitationRecordDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserEquipmentDao userEquipmentDao;
    @Autowired
    private EquipmentDao equipmentDao;

    /**
     * 邀请记录插入
     *
     * @param invitationRecord
     * @return
     */
    @Override
    public ResultInfo<?> save(InvitationRecord invitationRecord) {
        //初始化对象
        ResultInfo info = new ResultInfo();
        Boolean code = true;
        //被邀请者
        String inviter = invitationRecord.getInviter();
        try {
            if (invitationRecord.getId() != 0) {
                UserEquipment userEquipment = new UserEquipment();
                if (invitationRecord.getAcceptFlag() == 1) {
                    String equipmentCode = invitationRecord.getEquipmentCode();
                    invitationRecord.setStatus(TokenConstant.ACCEPTED);
                    userEquipment.setUserID(invitationRecord.getInviter());
                    userEquipment.setCreateUser(invitationRecord.getInviter());
                    userEquipment.setEquipmentCode(equipmentCode);
                    userEquipment.setOwn(TokenConstant.ONE);
                    int count = userEquipmentDao.getUserEquipmentByNameAndId(userEquipment);
                    if (count == 0) {
                        userEquipmentDao.insertUserEquipment(userEquipment);
                    }
                }
                invitationRecordDao.updateStaus(invitationRecord);

            } else {
                if (invitationRecord.getAcceptFlag() == 0) {
                    invitationRecord.setStatus(TokenConstant.ACCEPT);
                }
                String[] eqCode = invitationRecord.getEquipmentCodes();
                if (eqCode.length > 0) {
                    for (int j = 0; j < eqCode.length; j++) {
                        User iuser = new User();
                        Equipment equipment = equipmentDao.getEquipmentById(eqCode[j]);
                        //获取用户信息
                        User user = userDao.getByUserInfor(invitationRecord.getUserID());
                        iuser.setLoginName(inviter);
                        iuser.setUserID(invitationRecord.getUserID());
                        invitationRecord.setContent(user.getUserName() + TokenConstant.CONDITION + equipment.getEquipmentName());
                        User userInviter = userDao.getByUser(iuser);
                        if (userInviter == null) {
                            code = false;
                        } else {
                            invitationRecord.setEquipmentCode(eqCode[j]);
                            invitationRecord.setInviter(userInviter.getUserID());
                            int count = invitationRecordDao.queryRecord(invitationRecord);
                            if (count == 0) {
                                //插入信息
                                invitationRecordDao.save(invitationRecord);
                            }
                        }
                    }
                    if (!code) {
                        info.setMsg(ResultConstant.USER_NOT_FOUND.getMessage());
                        info.setCode(ResultConstant.USER_NOT_FOUND.getCode());
                    } else {
                        //返回成功编码
                        info.setMsg(ResultConstant.SUCCESS.getMessage());
                        info.setCode(ResultConstant.SUCCESS.getCode());
                    }

                }

            }

        } catch (
                Exception e)

        {
            info.setMsg(ResultConstant.FAIL.getMessage());
            info.setCode(ResultConstant.FAIL.getCode());
        }

        return info;
    }

    /**
     * 邀请列表
     *
     * @param t
     * @return
     */
    @Override
    public PageInfo<InvitationRecord> findAllByPage(InvitationRecord t) {
        PageInfo<InvitationRecord> info = null;
        try {
            // 分页
            Page<InvitationRecord> tPage = PageHelper.startPage(t.getPageNumber(), t.getPageSize());
            // 获取列表
            List<InvitationRecord> findRecordList = invitationRecordDao.findAllByPage(t);
            if (findRecordList.size() > 0) {
                for (int i = 0; i < findRecordList.size(); i++) {
                    InvitationRecord invitationRecord = findRecordList.get(i);
                    long day = DateUtil.getDaySub(invitationRecord.getCreateDate(), DateUtil.getTime());
                    if (day > 2) {
                        InvitationRecord re = new InvitationRecord();
                        re.setStatus(TokenConstant.EXPIRED);
                        re.setId(invitationRecord.getId());
                        invitationRecordDao.updateStaus(re);
                    }
                }
            }
            List<InvitationRecord> recordList = invitationRecordDao.findAllByPage(t);
            // 获取返回分页数据
            info = new PageInfo(recordList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }
}
